<?php


namespace app\api\controller\v26;


use app\api\controller\v2\Auth;
use app\common\cachemodel\ZlCouponActive;
use app\common\webmodel\WebSixOneActive2021;
use XznsTools\cache\CacheManage;

class SixOneActive extends Auth
{
    public function giveCoupon()
    {
        $activeInfo = (new WebSixOneActive2021())->activeInfo($this->user['id']);

        switch (true)
        {
            case $activeInfo['score'] >= 10 && $activeInfo['v1'] < 1:
                $activeId = 27; // 能力值10
                $discount = 0.9;
                $level = 'v1';
                break;
            case $activeInfo['score'] >= 30 && $activeInfo['v2'] < 1:
                $activeId = 26; // 能力值30
                $discount = 0.85;
                $level = 'v2';
                break;
            case $activeInfo['score'] >= 50 && $activeInfo['v3'] < 1:
                $activeId = 25; // 能力值50
                $discount = 0.8;
                $level = 'v3';
                break;
            case $activeInfo['score'] >= 70 && $activeInfo['v4'] < 1:
                $activeId = 24; // 能力值70
                $discount = 0.75;
                $level = 'v4';
                break;
            case $activeInfo['score'] >= 90 && $activeInfo['v5'] < 1:
                $activeId = 23; // 能力值90
                $discount = 0.7;
                $level = 'v5';
                break;
            case $activeInfo['score'] >= 100:
                if ($activeInfo['v6'] < 1)
                {
                    $activeId = 22; // 能力值100
                    $discount = 0.6;
                    $level = 'v6';
                } else
                {
                    return show(config('code.error'), '优惠券全部领取完了～');
                }
                break;
            default:
                return show(config('code.error'), '暂无优惠券可领～');
        }

        $couponActiveModel = new ZlCouponActive();

        $res = $couponActiveModel->couponGiveActiv($this->user['id'], $activeId);

        if ($res)
        {
            // 记录领取信息
            (new WebSixOneActive2021())->updateData($this->user['id'], (int)$activeInfo['id'], [$level => 1, 'give_num' => $activeInfo['give_num'] + 1]);
            // 写入领取记录
            $this->giveRecordCacheAdd($this->user,$activeId);

            return show(config('code.success'), '领取成功',['discount'=>$discount]);
        }

        return show(config('code.error'), $couponActiveModel->errorMsg);
    }

    /**
     * 记录领取记录
     * @param array $userinfo
     * @param int $id
     */
    private function giveRecordCacheAdd(array $userinfo, int $id): void
    {
        $len = 15;
        // 获取redis操作对象
        $redis = CacheManage::getInstance()->redisHandler;

        $cacheKey = 'active:six_ten:give:info';
        $num = $redis->lLen($cacheKey);

        if ($num >= $len)
        {
            $redis->rPop($cacheKey);
        }

        $info = [
            'nickname' => $userinfo['nickname'],
            // 'avatar' => $userinfo['avatar'],
            'id' => $id,
        ];

        $record = \GuzzleHttp\json_encode($info, JSON_UNESCAPED_UNICODE);

        $redis->lPush($cacheKey, $record);
    }
}
